package com.cspg.design.ui.project;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.ActivityChooserView;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.cspg.design.CSPGApplication;
import com.cspg.design.R;
import com.cspg.design.bean.deviceBean.PointDeviceBean;
import com.cspg.design.bean.deviceBean.PolylineDeviceBean;
import com.cspg.design.bean.modelLibrary.TowerLibrary;
import com.cspg.design.bean.modelLibrary.WireLibrary;
import com.cspg.design.bean.projectBean.ProjectBean;
import com.cspg.design.bean.shemeBean.MyHighVoltageSchemeBean;
import com.cspg.design.dao.gen.DaoSession;
import com.cspg.design.dao.gen.MyHighVoltageSchemeBeanDao;
import com.cspg.design.dao.gen.PointDeviceBeanDao;
import com.cspg.design.dao.gen.PolylineDeviceBeanDao;
import com.cspg.design.dao.gen.ProjectBeanDao;
import com.cspg.design.dao.gen.TowerLibraryDao;
import com.cspg.design.dao.gen.WireLibraryDao;
import com.cspg.design.request.PointDevice;
import com.cspg.design.utils.ActionBarUtil;
import com.cspg.design.utils.StatusBarUtil;
import com.cspg.design.widget.AlertListDialog;
import com.cspg.design.widget.ToastUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PoleAndWireActivity extends AppCompatActivity {
    TableLayout poleTable ;
    TableLayout wireTable;
    Button      poleBtn;
    Button      wireBtn;
    DaoSession daoSession;
    List<PointDeviceBean> pointDeviceBeanList;
    List<PolylineDeviceBean> polylineDeviceBeanList;
    ProjectBean    projectBean;
    Map<Integer, PointDeviceBean> pointDeviceBeanMap = new HashMap<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pole_and_wire);
        StatusBarUtil.setStatusBarColor(this,R.color.colorPrimary);
        ActionBarUtil.setActionBar(this,R.string.project_total);
        LinearLayout mainLinearLayout = findViewById(R.id.ll_pole_wire);
        View wireView = LayoutInflater.from(this).inflate(R.layout.pole_wire_wire,null);
        View poleView = LayoutInflater.from(this).inflate(R.layout.pole_wire_pole,null);
        mainLinearLayout.addView(wireView);

        poleTable = poleView.findViewById(R.id.tl_pole_wire_pole);
        wireTable = wireView.findViewById(R.id.tl_pole_wire_wire);

        poleBtn = poleView.findViewById(R.id.btn_pole_wire_pole_save);
        poleBtn.setOnClickListener((View v)->{
            Collection<PointDeviceBean> pointDeviceBeanCollection = pointDeviceBeanMap.values();
            try {
                daoSession.getPointDeviceBeanDao().saveInTx(pointDeviceBeanCollection);
                ToastUtils.toast(PoleAndWireActivity.this,"保存成功。。。。");
            }catch (Exception e)
            {
                ToastUtils.toast(PoleAndWireActivity.this,"保存出错。。。。");
            }
        });

        TextView tabPole = findViewById(R.id.tv_pole_wire_pole);
        TextView tabWire = findViewById(R.id.tv_pole_wire_wire);

        tabPole.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tabPole.setTextColor(Color.BLUE);
                tabWire.setTextColor(getResources().getColor(android.R.color.darker_gray));
                mainLinearLayout.removeAllViews();
                mainLinearLayout.addView(poleView);
            }
        });

        tabWire.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tabWire.setTextColor(Color.BLUE);
                tabPole.setTextColor(getResources().getColor(android.R.color.darker_gray));
                mainLinearLayout.removeAllViews();
                mainLinearLayout.addView(wireView);
            }
        });
        initData();
        initUI();

    }

    private void initData()
    {
        daoSession = ((CSPGApplication)getApplication()).getDaoSession();
        String projectId = getIntent().getStringExtra("projectId");
        projectBean = daoSession.getProjectBeanDao().queryBuilder().where(ProjectBeanDao.Properties.ProjectId.eq(projectId)).unique();
        pointDeviceBeanList = daoSession.getPointDeviceBeanDao().queryBuilder()
                .where(PointDeviceBeanDao.Properties.ProjectId.eq(projectId),PointDeviceBeanDao.Properties.PointType.eq(1))
                .list();
        polylineDeviceBeanList = daoSession.getPolylineDeviceBeanDao().queryBuilder()
                .where(PolylineDeviceBeanDao.Properties.ProjectId.eq(projectId))
                .list();
    }
    private void initUI()
    {
        if (pointDeviceBeanList != null)
        {
            for (int i = 0 ; i< pointDeviceBeanList.size();i++)
            {
                PointDeviceBean pointDeviceBean = pointDeviceBeanList.get(i);
                List<TowerLibrary> towerLibrarys = daoSession.getTowerLibraryDao()
                        .queryBuilder().where(TowerLibraryDao.Properties.PoleModel.eq(pointDeviceBean.getPointModel()))
                        .list();
                TowerLibrary towerLibrary = new TowerLibrary();
                if (towerLibrarys.size() > 0)
                {
                    towerLibrary = towerLibrarys.get(0);
                }
                View tableRowItem = LayoutInflater.from(this).inflate(R.layout.pole_wire_pole_tablerow_item,null);
                TextView modelTV = tableRowItem.findViewById(R.id.pole_wire_pole_model);
                TextView codeTV = tableRowItem.findViewById(R.id.pole_wire_pole_code);
                TextView typeTV = tableRowItem.findViewById(R.id.pole_wire_pole_type);
                TextView statusTV = tableRowItem.findViewById(R.id.pole_wire_pole_status);
                modelTV.setText(pointDeviceBean.getPointModel());
                modelTV.setTag(i);
                codeTV.setText(pointDeviceBean.getPointName());
                typeTV.setText(towerLibrary.getWireQuality());
                statusTV.setTag(i);
                switch (pointDeviceBean.getPointStatue())
                {
                    case 1:
                        statusTV.setText("新增");
                        break;
                    case 2:
                        statusTV.setText("原有");
                        break;
                    case 3:
                        statusTV.setText("换装");
                        break;
                    case 4:
                        statusTV.setText("拆除");
                        break;
                }
                modelTV.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (projectBean!= null)
                        {
                            String schemeName = projectBean.getSchemeName();
                            String schemeType = projectBean.getSchemaType();
                            if (schemeType.equals("high"))
                            {
                               MyHighVoltageSchemeBean myHighVoltageSchemeBean = daoSession.getMyHighVoltageSchemeBeanDao()
                                       .queryBuilder().where(MyHighVoltageSchemeBeanDao.Properties.SchemeName.eq(schemeName),
                                               MyHighVoltageSchemeBeanDao.Properties.UserName.eq(projectBean.getUserName()))
                                       .unique();
                               String poleStr = myHighVoltageSchemeBean.getTypicalDesignModel();
                               String[] poles = poleStr.split("&");
                                new AlertListDialog.ListBuilder(PoleAndWireActivity.this)
                                        .setDividerHeightRes(R.dimen.dp1)
                                        .setDividerColorRes(R.color.color_divider)
                                        .setTitle("请选择杆塔")
                                        .setIcon(R.drawable.ic_info)
                                        .setCancelable(true)
                                        .setItems(poles, (dialog, which) -> {
                                            modelTV.setText(poles[which]);
                                            PointDeviceBean pointDeviceBean1 = pointDeviceBeanList.get((Integer) modelTV.getTag());
                                            pointDeviceBean1.setPointModel(poles[which]);
                                            pointDeviceBeanMap.put((Integer) modelTV.getTag(),pointDeviceBean1);
                                        }).create().show();
                            }
                        }
                    }
                });
                statusTV.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String[] operation = new String[]{"新增","原有","换装","拆除"};
                        new AlertListDialog.ListBuilder(PoleAndWireActivity.this)
                                .setDividerHeightRes(R.dimen.dp1)
                                .setDividerColorRes(R.color.color_divider)
                                .setTitle("请操作")
                                .setIcon(R.drawable.ic_info)
                                .setCancelable(true)
                                .setItems(operation,
                                        (dialog, which) -> {
                                            statusTV.setText(operation[which]);
                                            PointDeviceBean pointDeviceBean1 = pointDeviceBeanList.get((Integer) statusTV.getTag());
                                            pointDeviceBean1.setPointStatue(which+1);
                                            pointDeviceBeanMap.put((Integer) statusTV.getTag(),pointDeviceBean1);
                                        }).create().show();
                    }
                });
                poleTable.addView(tableRowItem);
            }
        }
        if (polylineDeviceBeanList != null)
        {
            for (int i = 0 ;i<polylineDeviceBeanList.size();i++)
            {
                PolylineDeviceBean polylineDeviceBean = polylineDeviceBeanList.get(i);
                List<WireLibrary> wireLibrarys = daoSession.getWireLibraryDao().queryBuilder().where(WireLibraryDao.Properties.WireModel.eq(polylineDeviceBean.getPolylineModel())).list();
                WireLibrary wireLibrary = wireLibrarys.size()> 0 ? wireLibrarys.get(0) : null;
                if(wireLibrary == null)
                {
                    continue;
                }
                View tableRowItem = LayoutInflater.from(this).inflate(R.layout.pole_wire_wire_tablerow_item,null);
                TextView modelTV = tableRowItem.findViewById(R.id.pole_wire_wire_model);
                TextView startTV = tableRowItem.findViewById(R.id.pole_wire_wire_start);
                TextView endTV   = tableRowItem.findViewById(R.id.pole_wire_wire_end);
                TextView typeTV  = tableRowItem.findViewById(R.id.pole_wire_wire_type);
                modelTV.setText(polylineDeviceBean.getPolylineModel());
                startTV.setText(polylineDeviceBean.getStartPointName());
                endTV.setText(polylineDeviceBean.getEndPointName());
                typeTV.setText(wireLibrary.getWireQuality());
                wireTable.addView(tableRowItem);
            }
        }
    }
}